package com.gl.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gl.order.entity.CustomerAddressRef;
import com.gl.order.entity.ShippingAddress;
import com.gl.order.entity.SystemDict;
import com.gl.order.entity.TPermission;
import com.gl.order.entity.dto.ShippingAddressDto;
import com.gl.order.entity.vo.ShippingAddressVo;
import com.gl.order.mapper.CustomerAddressRefMapper;
import com.gl.order.mapper.ShippingAddressMapper;
import com.gl.order.service.ShippingAddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ShippingAddressServiceImpl extends ServiceImpl<ShippingAddressMapper, ShippingAddress> implements ShippingAddressService {

    @Autowired
    private CustomerAddressRefMapper customerAddressRefMapper;

    @Override
    public void addShippingAddress(ShippingAddressDto dto){
        ShippingAddress shippingAddress = new ShippingAddress();
        BeanUtils.copyProperties(dto, shippingAddress);
        shippingAddress.setCreateTime(LocalDateTime.now());
        shippingAddress.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(shippingAddress);
        CustomerAddressRef customerAddressRef = new CustomerAddressRef();
        customerAddressRef.setCustomerId(dto.getCustomerId());
        customerAddressRef.setAddressId(shippingAddress.getId());
        customerAddressRefMapper.insert(customerAddressRef);
    }

    @Override
    public List<ShippingAddressVo> findByCustomerId(String customerId) {
        if(StringUtils.isNotBlank(customerId)){
            return this.baseMapper.findByCustomerId(customerId);
        }
        return new ArrayList();
    }

    @Override
    public List<ShippingAddressVo> findByCustomer(List<String> customerIds) {
        return this.baseMapper.findByCustomer(customerIds);
    }

}
